//
//  A2ViewController.m
//  Lesson2_Controllers
//
//  Created by joanfen on 2016/12/8.
//  Copyright © 2016年 芳仔小脚丫. All rights reserved.
//

#import "A2ViewController.h"

@interface A2ViewController ()

@end

@implementation A2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)popToA2:(id)sender {
    // 1. A0 是navigationController 的根视图
    // 直接 pop 到 rootViewController 即可
    
    [self.navigationController popToRootViewControllerAnimated:YES];

/*
    // 2. 如果这里视图层级再多几级，A0不是根视图的话上面的方法就做不到了，我们可以根据 index
    // 这里 A0 是 视图堆栈中的倒数第三个视图
    NSArray<UIViewController *> *viewControllersOfNavi = self.navigationController.viewControllers;
    NSInteger index = viewControllersOfNavi.count - 3;
    if (index < viewControllersOfNavi.count) {
         [self.navigationController popToViewController:viewControllersOfNavi[index] animated:YES];
    }
   
    // 3. 还可以通过遍历的方式来 pop
    [viewControllersOfNavi enumerateObjectsUsingBlock:^(UIViewController  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 这里我们要跳转的 A0，是没有为其添加类的，如果有类别的话，我们可以通过判断类的方法，比如 A0 的类为 A0ViewController
        if ([obj isKindOfClass:NSClassFromString(@"A0ViewController")]) {
            [self.navigationController popToViewController:obj animated:YES];
            *stop = YES;
        }
        // 还可以通过其他判断条件比如 A0的 title 是 `A0`
        if ([obj.title isEqualToString:@"A0"]) {
            [self.navigationController popToViewController:obj animated:YES];
            *stop = YES;
        }
        // 任何你觉得可以判断 obj 为 A0的方式都可以作为判断条件
    }];
*/
    
}
- (IBAction)toB0:(id)sender {
    self.tabBarController.selectedIndex = 1;
     [self.navigationController popToRootViewControllerAnimated:NO];
}

@end
